package com.fit.dao.manufacturer;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.Manufacturer;

public class ManufacturerDAOImpl implements ManufacturerDAO {

	@Inject
	private Session session;

	public boolean save(Manufacturer manufacturer) {
		if (manufacturer != null) {
			session.save(manufacturer);
			return true;
		}
		return false;
	}

	public Manufacturer findById(Long id) {
		ArrayList<Manufacturer> list = (ArrayList<Manufacturer>) session
				.createCriteria(Manufacturer.class)
				.add(Restrictions.eq("id", id)).list();
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Manufacturer> findAll() {
		return session.createCriteria(Manufacturer.class).list();
	}

	public boolean deleteById(Long id) {
		Manufacturer m = findById(id);
		if (m != null) {
			session.delete(m);
			return true;
		}
		return false;
	}

	public boolean update(Manufacturer manufacturer) {
		if (manufacturer != null) {
			session.update(manufacturer);
			return true;
		}
		return false;
	}

//	public Manufacturer findByIdAndCompany(Long id, String company) {
//		if (id == 0 || company == null || company.equals("")) {
//			return null;
//		}
//
//
//		List l = session.createCriteria(Manufacturer.class)
//				.add(Restrictions.eq("id", id))
//				.add(Restrictions.eq("company", company)).list();
//		Manufacturer m = null;
//		if (l != null) {
//			if (l.size() > 0) {
//				m = (Manufacturer) l.get(0);
//			}
//		}
//		return m;
//	}


}
